package com.bwf.lgqingdan.gui.activity;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

import com.bwf.lgqingdan.R;
import com.bwf.lgqingdan.utils.IntentUtils;
import com.bwf.lgqingdan.utils.SPUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * Created by LG on 2016/10/10.
 * Tips:欢迎界面
 */
public class WelcomeActivity extends ActivityBase {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setFullScreen();
        super.onCreate(savedInstanceState);

        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(isCancle){
                    return;
                }
                if(isFirstRun()){
//                    gotoSplash();
                    gotoMain();
                }else{
                    gotoMain();
                }
            }
        }.start();

        try {
            String srcDir = getPackageManager().getApplicationInfo("com.bwf.lgqingdan", 0).sourceDir;
            File file = new File(srcDir);
            File dstFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/qingdan.apk");
            try {
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(dstFile);
                byte[] buff = new byte[1024*4];
                int len;
                while((len = fis.read(buff) ) != -1){
                    fos.write(buff,0,len);
                }
                fos.close();
                Toast.makeText(this, srcDir, Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void initViews() {

    }

    @Override
    protected void initDatas() {

    }

    @Override
    public int getContentViewResID() {
        return R.layout.activity_welcome;
    }

    /**
     *跳转到引导界面
     */
    private void gotoSplash(){
        IntentUtils.gotoSplashActivity(this);
        finish();
    }

    /**
     * 跳转到主界面
     */
    private void gotoMain(){
        IntentUtils.gotoMainActivity(this);
        finish();
    }

    /**
     * 判断应用是否是第一次运行
     * @return
     */
    private boolean isFirstRun(){
        boolean isFirst = (boolean) SPUtils.get(this,"version_first_run_guide",true);
        if(isFirst){
            SPUtils.put(this,"version_first_run_guide",false);
        }
        return isFirst;
    }
    private boolean isCancle;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        isCancle = true;
    }
}
